package com.dao;
/***
*
*/
import com.util.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class CartscoffoodsDao {
	public CartscoffoodsDao(){
	
	}
	public int addCartscoffoods(HttpServletRequest req){
		String cartid=req.getParameter("cartid")==null?"":req.getParameter("cartid");
		String foodsid=req.getParameter("foodsid")==null?"":req.getParameter("foodsid");
		String scid=req.getParameter("scid")==null?"":req.getParameter("scid");
		String sql ="insert into TBL_CARTSCOFFOODS("+
        		"CARTID"+
        		",FOODSID"+
        		",SCID"+
        	") values("+
        		"'"+cartid+"'"+
        	        		",'"+foodsid+"'"+
        	        		",'"+scid+"'"+
        	")";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public int delCartscoffoods(HttpServletRequest req){
		String sql ="delete from TBL_CARTSCOFFOODS where 1=1 "+
    	"";
    	DB db = new DB();
    	return db.executeSql(sql);	
	}
	public int updCartscoffoods(HttpServletRequest req){
		String cartid=req.getParameter("cartid")==null?"":req.getParameter("cartid");
		String foodsid=req.getParameter("foodsid")==null?"":req.getParameter("foodsid");
		String scid=req.getParameter("scid")==null?"":req.getParameter("scid");
		String sql ="update TBL_CARTSCOFFOODS set "+
        		"CARTID='"+cartid+"'"+
        		",FOODSID='"+foodsid+"'"+
        		",SCID='"+scid+"'"+
        	" where 1=1 "+
    	"";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public Map<String,Object> findCartscoffoodsById(HttpServletRequest req){
		String sql ="select * from TBL_CARTSCOFFOODS where 1=1 "+
    	"";
    	DB db = new DB();
    	Map<String,Object> map = null;
    	List<Map<String,Object>> list = db.query(sql);
    	if(list!=null&&list.size()>0){
    		map = list.get(0);
    	}
    	return map;	
	}
	
	
	public PageModel findCartscoffoodsByCondition(HttpServletRequest req){
		String cartid=req.getParameter("cartid")==null?"":req.getParameter("cartid");
		req.setAttribute("cartid", cartid);
		String foodsid=req.getParameter("foodsid")==null?"":req.getParameter("foodsid");
		req.setAttribute("foodsid", foodsid);
		String scid=req.getParameter("scid")==null?"":req.getParameter("scid");
		req.setAttribute("scid", scid);
		String sql ="";
		String where="";
		if(cartid!=null&&!"".equals(cartid)){
    		where+=" and CARTID like '%"+cartid+"%'";
		}
		if(foodsid!=null&&!"".equals(foodsid)){
    		where+=" and FOODSID like '%"+foodsid+"%'";
		}
		if(scid!=null&&!"".equals(scid)){
    		where+=" and SCID like '%"+scid+"%'";
		}
    	String countsql ="select count(*) from TBL_CARTSCOFFOODS where 1=1 "+where;
    	PageModel model = new PageModel();
		String tempPage = req.getParameter("currentPage");
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(tempPage);
		} catch (NumberFormatException e) {
			currentPage=1;
		}
		model.setCurrentPage(currentPage);
		String tempCount = req.getParameter("pageCount");
		int pageCount = 20;
		try {
			pageCount = Integer.parseInt(tempCount);
		} catch (NumberFormatException e) {
			pageCount=20;
		}
		model.setPageCount(pageCount);
		sql="select  * from TBL_CARTSCOFFOODS where 1=1 "+where+" order by  desc limit "+(currentPage-1)+","+pageCount+"";
    	DB db = new DB();
    	int count = Integer.parseInt(db.queryFirst(countsql));
    	model.setTotalCount(count);
    	if(count>0){
    		model.setRes(db.query(sql));
    	}
    	return model;	
	}
	
}